package com.bidevalution.business.impl;

import java.util.List;
import java.util.Map;



import com.google.common.collect.Maps;
import com.qtp.core.common.RemoteService;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.IdsUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.bidevalution.business.service.BeSectionSignService;
import com.bidevalution.dao.entity.BeSectionSign;
import com.bidevalution.dao.mapper.BeSectionSignMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author admin
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BeSectionSignServiceImpl implements BeSectionSignService {
    @Autowired
    private BeSectionSignMapper beSectionSignMapper;

    @Value("${ids.url}")
    private String ptIdsUrl;

    @Override
    public List<BeSectionSign> selectList(String sectionId) {
        return beSectionSignMapper.selectList(sectionId);
    }

    @Override
    public Integer sign(String sectionId, String ptSectionId, String orgCode, String enterpriseId) throws Exception {
        //获取第二个数据源
        
        BeSectionSign beSectionSign = new BeSectionSign();
        List<BeSectionSign> beSectionSignList = beSectionSignMapper.selectList(sectionId);
        if (beSectionSignList != null) {
            beSectionSign.setSeatNo(beSectionSignList.size() + 1);
        } else {
            beSectionSign.setSeatNo(0);
        }
        beSectionSign.setSeqId(IdsUtil.uuid());
        beSectionSign.setCompanyId(enterpriseId);
        beSectionSign.setSectionId(sectionId);
        if (StringUtils.isBlank(ptIdsUrl)) {
            throw new Exception("未配置平台ids地址");
        }
        Map<String, Object> params = Maps.newHashMap();
        params.put("bsID", ptSectionId);
        params.put("busiType", "1");
        String bidViewUrl = ptIdsUrl + "/bidViewWz.shtml";
        //调用接口
        String response = RemoteService.getRemoteInfo(bidViewUrl, params);
        if (StringUtils.isNotBlank(response)) {
            Map<String, Object> resultMap = GsonUtil.jsonToMapWithGson(response);
            if (resultMap != null) {
                List<Map<String, String>> list = (List<Map<String, String>>) resultMap.get("bids");
                if (list != null) {
                    for (Map<String, String> map : list) {
                        if (map != null && map.get("orgCode") != null && map.get("orgCode").contains(orgCode)) {
                            if (map.get("fileID") == null) {
                                throw new Exception("投标文件未上传！");
                            } else {
                                beSectionSign.setCompanyOrgCode(orgCode);
                                beSectionSign.setCompanyName(map.get("orgName"));
                                beSectionSign.setPtBidId(map.get("orgGUID"));
                                beSectionSign.setPayDeposit(String.valueOf(map.get("payedBail")));
                                break;
                            }
                        }
                    }
                }
            }
        }
        if(StringUtils.isNotBlank(beSectionSign.getCompanyOrgCode())){
            if(isSigned(sectionId, orgCode)){
                return 1;
            } else {
                return beSectionSignMapper.insert(beSectionSign);
            }
        } else {
            throw new Exception("未查询到投标单位报名相关信息");
        }
    }

    @Override
    public Long querySignCount(String sectionId) {
        //获取第二个数据源
        
        List<BeSectionSign> beSectionSignList = beSectionSignMapper.selectList(sectionId);
        if (beSectionSignList != null) {
            return Long.valueOf(beSectionSignList.size());
        }
        return 0L;
    }

    /**
     * 判断投标单位是否已经签到
     * @param sectionId
     * @param orgCode
     * @return
     */
    @Override
    public boolean isSigned(String sectionId, String orgCode) {
        //获取第二个数据源
        
        BeSectionSign beSectionSign = beSectionSignMapper.selectBeSectionSign(sectionId, orgCode);
        if (beSectionSign != null) {
            return true;
        }
        return false;
    }
}
